package com.dragon.modules.yuegou.service.impl.busd;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.busd.WithdrawConfigEntity;
import com.dragon.modules.yuegou.mapper.busd.WithdrawConfigMapper;
import com.dragon.modules.yuegou.model.busd.WithdrawConfig;
import com.dragon.modules.yuegou.query.busd.WithdrawConfigQuery;
import com.dragon.modules.yuegou.service.busd.WithdrawConfigService;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商圈提现设置 服务实现
 *
 * @author pbuilder generated
 * @date 2019-07-30 16:36:57
 * @version 2.0
 */
@Service("busdWithdrawConfigService")
public class WithdrawConfigServiceImpl
		extends ServiceImpl<WithdrawConfigMapper, WithdrawConfigEntity>
		implements WithdrawConfigService {

	@Override
	public void setWithdraw(WithdrawConfig withdraw){
		//数据校验
		checkData(withdraw);
		//提现设置
		baseMapper.updateById(withdraw);
	}

	@Override
	public WithdrawConfig getByBusdId(String busdId) {
		WithdrawConfigQuery query = new WithdrawConfigQuery();
		query.busdId().eq(busdId);
		List<WithdrawConfigEntity> withdrawConfigs = list(query.toQueryWrapper());
		if (CollectionUtils.isNotEmpty(withdrawConfigs)){
			return new WithdrawConfig(withdrawConfigs.get(0));
		}
		return new WithdrawConfig();
	}

	private void checkData(WithdrawConfig withdraw){
		Check.notEmpty(withdraw.getId(), "id不能为空");
		Check.notNull(withdraw.getWithdrawTimes(), "每月提现次数不能为空");
		Check.digits(withdraw.getWithdrawTimes(), 10, 0, "每月提现次数格式不正确");
		Check.notNull(withdraw.getWithdrawDuration(), "可提现时长不能为空");
		Check.digits(withdraw.getWithdrawDuration(), 10, 0, "可提现次数格式不正确");
		Check.notNull(withdraw.getWithdrawMinAmount(), "最小金额不能为空");
		Check.positive(withdraw.getWithdrawMinAmount(), "最小金额必须为正数");
		Check.digits(withdraw.getWithdrawMinAmount(), 10, 2, "最小金额格式不正确");
		Check.notNull(withdraw.getWithdrawMaxAmount(), "最大金额不能为空");
		Check.positive(withdraw.getWithdrawMaxAmount(), "最大金额必须为正数");
		Check.digits(withdraw.getWithdrawMaxAmount(), 10, 2, "最大金额格式不正确");
		Check.notNull(withdraw.getFeeRatio(), "手续费比例不能为空");
		Check.positive(withdraw.getFeeRatio(), "手续费比例必须为正数");
		Check.digits(withdraw.getFeeRatio(), 5, 2, "服务费比例格式不正确");
		Check.notNull(withdraw.getServiceFeeRatio(), "服务费比例不能为空");
		Check.positive(withdraw.getServiceFeeRatio(), "服务费比例必须为正数");
		Check.digits(withdraw.getServiceFeeRatio(), 5, 2, "服务费比例格式不正确");
		Check.notNull(withdraw.getType(), "服务费收取方式不能为空");
	}
}